<?php

namespace App\Remote\FuYou;

use Closure;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

class Remote extends \App\Remote\Remote
{
    /**
     * @inheritDoc
     */
    static function getBaseUri(): string
    {
        // TODO: Implement getBaseUri() method.
        return "https://scan-rim-mc.fuioupay.com";
//        return "https://scan-rim-test.fuioupay.com";
    }

    public static function buildBasicHeaders(): array
    {
        return ["Content-Type" => "multipart/form-data"]; // TODO: Change the autogenerated stub
    }

    public static function registerResponseHandle(): Closure
    {
        return function () {
            list($responseHttpCode, $responseContent) = func_get_args();
            if ($responseHttpCode != ResponseAlias::HTTP_OK) {
                throw new \Exception($responseContent);
            }

            return json_decode(json_encode(simplexml_load_string(urldecode($responseContent))), true);
        };
    }

    /**
     * 获取机构号
     *
     * @return string
     */
    public static function getInsCd(): string
    {
        return config('pay.fy.insCd');//prod
//        return "08A9999999";//qa
    }


    /**
     * 获取私钥
     *
     * @return string
     */
    public static function getPrivateKey(): string
    {
        //qa
//        $privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJgAzD8fEvBHQTyxUEeK963mjziMWG7nxpi+pDMdtWiakc6xVhhbaipLaHo4wVI92A2wr3ptGQ1/YsASEHm3m2wGOpT2vrb2Ln/S7lz1ShjTKaT8U6rKgCdpQNHUuLhBQlpJer2mcYEzG/nGzcyalOCgXC/6CySiJCWJmPyR45bJAgMBAAECgYBHFfBvAKBBwIEQ2jeaDbKBIFcQcgoVa81jt5xgz178WXUg/awu3emLeBKXPh2i0YtN87hM/+J8fnt3KbuMwMItCsTD72XFXLM4FgzJ4555CUCXBf5/tcKpS2xT8qV8QDr8oLKA18sQxWp8BMPrNp0epmwun/gwgxoyQrJUB5YgZQJBAOiVXHiTnc3KwvIkdOEPmlfePFnkD4zzcv2UwTlHWgCyM/L8SCAFclXmSiJfKSZZS7o0kIeJJ6xe3Mf4/HSlhdMCQQCnTow+TnlEhDTPtWa+TUgzOys83Q/VLikqKmDzkWJ7I12+WX6A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ZDgAJgs7e0uDthGsEcYFKwcKF17fQKAogBRuUGStckTs16h4i7rXv9+o7nlpEbYCjtInP7CfPW0N9UJUTtxgDO1N4HDy7wBzmFAaIuyInf5H4gZ4EqntaKyG2eLwmEyYCitfVjio12TyGw1+/m5XFln0HzYSiglxG+p4wKBgQCjW5isWMUVi1PRzmZ0k4bkJGNq8cVX+P5QU12IJjHSF46jTwr6NdMngkbaVHNcd/VIPs8NmryY3puXwQekXULgfBA6XUpINWKpsWQ8MEORiidgPfi8uIXRye2jC5TUN7bwWQ8RM4lmSC6R+MXoW6g97PVR0YIdn9Tttoues1pNYQKBgDvJiAbE/9bkdI4HU/sdY+QyuemHJWhYoe/6zfcZyYuLn8waOJF2WaNQZ2Lt2XY3moCU+WD+8tQVdqricGgGtL0XQyhxmNG5SkQza7b8TEnOT2wJaUVoGjyBC+977Tp6loKReZyfVZ3KttDmIXZq93ImQbMJScFZBfvemD3IKrVPAoGAfFQfS7S4MyB6Z+k2Ey2g8Yw8Wk2uS7lHHS2FtzSSoHqrZeGn0i0bCyvc7xhtQIhQF28uOQV22sY6F3KjdVZBqwbhdvo/qdtvvwUy65g0JNXICDjCUigCOVv9UCVlXWsWAbFWhVn7DeyL/S8guSrhTlhFkcLpy9A0x8MyWlPYu6ECgYAL06RUt7apFDhBzSsEW6WRWyeIUyJy/dLGLxg/GQcilVJgiltFiIxIfooUvC9uDA+LoBfADZSKpYtFTW4aFC73PKlRnP2rDNz0lWvKFllcKe6GL5PO1amF9ybSE641RZavZ32a14B4VE9xwORj5y72W8UzqR+Ozf2Q2sspx/Cajg==";
        return vsprintf(
            "-----BEGIN RSA PRIVATE KEY-----%s%s%s-----END RSA PRIVATE KEY-----",
            [
                PHP_EOL,
                wordwrap(config('pay.fy.privateKey'), 64, PHP_EOL, true),
                PHP_EOL
            ]
        );
    }


    /**
     * 获取签名
     * @param $params
     * @return string
     */
    public static function getSign($params): string
    {
        ksort($params);
        $paramsString = http_build_query($params);
        $privateKey   = self::getPrivateKey();

        $privKeyId = openssl_get_privatekey($privateKey);
        openssl_sign($paramsString, $signature, $privKeyId, OPENSSL_ALGO_MD5);
        openssl_free_key($privKeyId);
        return base64_encode($signature);
    }

    /**
     * XML编码
     * @param mixed $data 数据
     * @param string $encoding 数据编码
     * @param string $root 根节点名
     * @return string
     */
    public static function toXml($data, $encoding = 'GBK', $root = 'xml'): string
    {
        $xml = '<?xml version="1.0" encoding="' . $encoding . '" standalone="yes"?>';
        $xml .= '<' . $root . '>';
        $xml .= self::data2xml($data);
        $xml .= '</' . $root . '>';
        return $xml;
    }

    /**
     * 数据XML编码
     * @param mixed $data 数据
     * @return string
     */
    public static function data2xml($data): string
    {
        $xml = '';
        foreach ($data as $key => $val) {
            is_numeric($key) && $key = "item id=\"$key\"";
            $xml .= "<$key>";
            $xml .= (is_array($val) || is_object($val)) ? self::data2xml($val) : $val;
            list($key,) = explode(' ', $key);
            $xml .= "</$key>";
        }
        return $xml;
    }
}
